<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入订单编号" clearable>
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- table -->
      <el-table :data="ordersList" border stripe @expand-change="getOrderDetail">
        <el-table-column label="明细" type="expand">
          <template slot-scope="scope">
            <el-table :data="scope.row.detail" border stripe>
              <el-table-column label="封面" width="100px" header-align="center" align="center">
                <template slot-scope="scope">
                  <!-- 实现图片放大效果 -->
                  <el-popover placement="top-start" trigger="hover">
                    <img :src="scope.row.url" style="width:150px;height:150px;">
                    <img :src="scope.row.url" slot="reference" style="width:50px;height:50px">
                  </el-popover>
                </template>
              </el-table-column>
              <el-table-column label="商品名称" prop="goods_name"></el-table-column>
              <el-table-column label="商品重量" prop="goods_weight" width="100px"></el-table-column>
              <el-table-column label="商品数量" prop="goods_number" width="100px"></el-table-column>
              <el-table-column label="商品价格" prop="goods_price" width="100px"></el-table-column>
              <el-table-column label="总价" prop="goods_total_price" width="100px"></el-table-column>
            </el-table>
          </template>
        </el-table-column>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格" prop="order_price"></el-table-column>
        <el-table-column label="是否付款">
          <template slot-scope="scope">
            <el-tag type="success" size="mini" effect="dark" v-if="scope.row.pay_status === '1'">已付款</el-tag>
            <el-tag type="danger" size="mini" effect="dark" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send"></el-table-column>
        <el-table-column label="下单时间" prop="create_time"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
            <el-button type="warning" size="mini" icon="el-icon-location" @click="showProgressDialog"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 物流 -->
    <el-dialog title="物流进度" :visible.sync="progressVisible" width="50%">
      <el-timeline>
        <el-timeline-item v-for="(activity, index) in progressInfo" :key="index" :timestamp="activity.time">
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>

  </div>
</template>

<script>
export default {
	data() {
		return {
			queryInfo: {
				query: '',
				pagenum: 1,
				pagesize: 10,
			},
			ordersList: [],
			total: 0,
			progressVisible: false,
			progressInfo: [],
		}
	},
	created() {
		this.getOrdersList()
	},
	methods: {
		async getOrdersList() {
			const { data: res } = await this.$http.get('orders', {
				params: this.queryInfo,
			})
			res.data.goods.forEach((item) => {
				item.detail = []
			})

			this.ordersList = res.data.goods
			this.total = res.data.total
		},
		async getOrderDetail(row) {
			const { data: res } = await this.$http.get(`orders/${row.order_id}`)
			// 为订单对象添加一个detail属性
			row.detail = res.data.goods

			// 获取订单下的商品列表
			row.detail.forEach(async (item) => {
				const { data: res } = await this.$http.get(
					`goods/${item.goods_id}`
				)
				this.$set(item, 'goods_name', res.data.goods_name)
				this.$set(item, 'goods_weight', res.data.goods_weight)
				this.$set(item, 'url', res.data.pics[0].pics_big_url)
			})
			console.log(row.detail)
		},
		async showProgressDialog() {
			const { data: res } = await this.$http.get(
				`/kuaidi/815294206237577`
			)
			this.progressInfo = res.data
			console.log(this.progressInfo)

			this.progressVisible = true
		},
	},
}
</script>

<style lang="less" scoped>
</style>